﻿# -*- coding:utf-8 -*-
#
# FileDownload 
# Скачивание файлов
#
import os
import Image
from django.template import Library
from django.conf import settings
import settings
from django.core.files.storage import FileSystemStorage
from mimetypes import guess_type as guess_mimetype
from urlparse import urljoin


class File:
    def __init__(self, name=None, path=None, isdir=None, size=None, modifed=None):
        self.name = name
        self.path = path
        self.isdir = isdir
        self.size = size
        self.modifed = modifed
    def __cmp__(self, other):
        return cmp(self.name, other.name)
        
        
        
class Directory(object):

    def __init__(self, rel_dir):
        self.rel_dir = (rel_dir+"/").lstrip('/')
        directory = os.path.join(settings.FILEDOWNLOAD_MEDIA_DIRECTORY, self.rel_dir)
        url = urljoin(settings.MEDIA_URL, self.rel_dir)
        self.s = FileSystemStorage(directory, url)


    def _get_has_child(self, path):
        ''' Наличие дочерних папок: True - есть, False - нет  '''
        for f in os.listdir(path):
            if os.path.isdir(os.path.join(path, f)):
                return True
        return False


    def collect_dirs_tree(self):
        ''' Полное дерево папок '''
        def childs(path):
            return [{'name': name,
                    'childs': childs(os.path.join(path, name)),
                    'rel_dir': os.path.relpath(os.path.join(path, name), self.s.path(''))}
                    for name in os.listdir(path)
                    if os.path.isdir(os.path.join(path, name))]
        return childs(self.s.path(''))


    def collect_dirs(self):
        ''' Список папок для tree-view '''
        path = self.s.path('')
        return [{   'name': name,
                    'childs': 0,
                    'rel_dir': os.path.join(self.rel_dir, name), #os.path.relpath(os.path.join(path, name), self.s.path('')),
                    'has_child': self._get_has_child(os.path.join(path, name)),}
                    for name in os.listdir(path)
                    if os.path.isdir(os.path.join(path, name))]


    def collect_files(self):
        ''' Список папок и файлов для list-view '''
        dirs, files = self.s.listdir('')
        # список папок
        dir_items = [{ 'isdir' : 1,
                       'name': d,  
                       'rel_dir': os.path.join(self.rel_dir, d),
                       'url': self.s.url(d)}
                     for d in dirs ]    
        # список файлов
        file_items = [{ 'isdir': 0,
                        'name': f, 
                        'size': self.s.size(f),
                        'rel_dir': os.path.join(self.rel_dir, f),
                        'date': self.s.created_time(f).ctime(),
                        'extension': os.path.splitext(f)[1],
                        'mimetype': guess_mimetype(f)[0] or "application/octet-stream",
                        'url': self.s.url(f)}
                      for f in files ]    
        # выводим все вместе
        return dir_items + file_items        